﻿using Handy_WPF.MVVM;
using HandyControl.Controls;
using Kxx_Domain.DNS;
using Kxx_Domain.Email;
using Kxx_Log.Log;
using Kxx_Save.DNS;
using Kxx_Task.DNS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Handy_DNS.ViewModel
{
    internal class APIConfigViewModel : NotificationProperty, IDisposable
    {
        #region 属性
        private APIConfig aliInfo;
        public APIConfig ALiInfo
        {
            get
            {
                return aliInfo;
            }
        }

        private QQEmailConfig qqEmailInfo;
        public QQEmailConfig QQEmailInfo
        {
            get
            {
                return qqEmailInfo;
            }
        }

        /// <summary>
        /// 本机IP地址
        /// </summary>
        public string NativeIPAddress
        {
            get
            {
                return DNSLoad.NativeIPAddress;
            }
        }
        #endregion

        public APIConfigViewModel()
        {
            if (BascConfig.ALiInfo == null)
            {
                aliInfo = new APIConfig();
            }
            else
            {
                aliInfo = BascConfig.ALiInfo;
            }

            if (BascConfig.QQEmailInfo == null)
            {
                qqEmailInfo = new QQEmailConfig();
            }
            else
            {
                qqEmailInfo = BascConfig.QQEmailInfo;
            }

            #region IP地址通知任务
            new Task(() => {
                int taskWaitTime = 3 * 1000;
                while (!_disposed)
                {
                    RaisePropertyChanged(nameof(NativeIPAddress));

                    Thread.Sleep(taskWaitTime);
                }
            }).Start();
            #endregion
        }

        #region 命令
        private BaseCommand dnsSaveCommand;
        /// <summary>
        /// DNS保存
        /// </summary>
        public BaseCommand DNSSaveCommand
        {
            get
            {
                if (dnsSaveCommand == null)
                {
                    dnsSaveCommand = new BaseCommand(new Action<object>(o =>
                    {
                        BascConfig.ALiInfo = this.ALiInfo;
                        Growl.Success("保存成功");
                        DNSLog.LogAdd(LogType.Success, "DNS接口配置保存", "保存成功");
                    }));
                }
                return dnsSaveCommand;
            }
        }

        private BaseCommand qqSaveCommand;
        /// <summary>
        /// QQ邮箱保存
        /// </summary>
        public BaseCommand QQSaveCommand
        {
            get
            {
                if (qqSaveCommand == null)
                {
                    qqSaveCommand = new BaseCommand(new Action<object>(o =>
                    {
                        BascConfig.QQEmailInfo = this.qqEmailInfo;
                        Growl.Success("保存成功");
                        DNSLog.LogAdd(LogType.Success, "QQEmail接口配置保存", "保存成功");
                    }));
                }
                return qqSaveCommand;
            }
        }
        #endregion

        #region 资源释放
        private bool _disposed;
        private object _obj = new object();
        public void Dispose()
        {
            Dispose(true);
            // 请求公共语言运行时不要调用该对象上的终结器（在C#中就是指不要调用析构函数）
            GC.SuppressFinalize(this);
        }

        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);
        protected virtual void Dispose(bool disposing)
        {
            // 保证线程安全
            lock (_obj)
            {
                if (!_disposed)
                {
                    if (disposing)
                    {
                        // Release managed resources
                    }

                    // Release unmanaged resources

                    _disposed = true;
                }
            }
        }
        public void Close()
        {
            Dispose();
        }
        ~APIConfigViewModel()
        {
            Dispose(false);
        }
        #endregion
    }
}
